Skip to main content

Chapter 26 - Null provider, time provider

The power of doing nothing and sleeping

Null Provider or Terraform_Data


Docs: https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource This is a construct that allows terraform to have a chance to do something on nothing.

The official documentation says that you can trigger this to run if an instance ID changes on a machine.

Time_sleep

This code here waits for the machine to create (the depends_on) and then will start a 90 second wait:

resource "time_sleep" "wait_90_seconds" {
depends_on = [azurerm_linux_virtual_machine.mylinuxvm]
create_duration = "90s"
}

Null resource


You an create a null resource, attach a connection block and then a file provisioner for this to copy some files over to the machine - you're not doing it on any particular resource, just treating it like a simple script job that you run on whatever resource is documented.

resource "null_resource" "sync_app_to_vm" {
# this is triggered after the 90 second wait
depends_on = [time_sleep.wait_90_seconds]
triggers = {
always-update = timestamp()
}

# Connect to the VM instance
connection {
type = "ssh"
host = azurerm_linux_virtual_machine.mylinuxvm.public_ip_address
user = azurerm_linux_virtual_machine.mylinuxvm.admin_username
private_key = file("${path.module}/ssh-keys/terraform-azure.pem")
}
# File Provisioner - Copies the app1 folder to the dest directory
provisioner "file" {
source = "apps/app1"
destination = "/tmp"
}

# Remote-Exec - Copies this folder to the destination on the remote.
provisioner "remote-exec" {
inline = [
"sudo cp -r /tmp/app1 /var/www/html"
]
}
}

image.png

Destroy time provisioner


timestamp